﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mofang.Utils.LogProvider
{
    /// <summary>
    /// 日志流水号生成器
    /// <para>--------------------------</para>
    /// <para>@Author: cnalexi@gmail.com</para>
    /// <para>@Create: 2016/4/15</para> 
    /// </summary>
    internal class SerialProvider
    {
        /// <summary>
        /// ID基数增长跨度
        /// </summary>
        private const int _idGap = 0x100;

        /// <summary>
        /// 产生随机尾数
        /// </summary>
        private static readonly Random _random = new Random((int)DateTime.Now.Ticks);

        /// <summary>
        /// 循环增长ID
        /// </summary>
        private static int _idBase;

        /// <summary>
        /// 标线时间 (2016)
        /// </summary>
        public static readonly DateTime _markTime = new DateTime(2016, 1, 1);

        /// <summary>
        /// 产生一个16进制的数字字符串(前补零)
        /// </summary>
        /// <param name="length">需要的长度</param>
        /// <returns></returns>
        private static string CreateUniqueString(int length)
        {
            if (length <= 0) return string.Empty;

            _idBase = (_idBase + _idGap) % 0x0fffff00;

            int result = _idBase + _random.Next(0xff);

            string str = result.ToString("X").PadLeft(length, '0');

            return str.Length > length ? str.Substring(str.Length - length, length) : str;
        }

        /// <summary>
        /// 产生一个10进制的数字字符串(前补随机数字串)
        /// </summary>
        /// <param name="length">需要的长度</param>
        /// <returns></returns>
        private static string CreateUniqueNumer(int length)
        {
            if (length <= 0) return string.Empty;

            _idBase = (_idBase + _idGap) % 0x0fffff00;

            int result = _idBase + _random.Next(0xff);

            string str = result.ToString("D");

            return str.Length > length ? str.Substring(str.Length - length, length) : str;
        }

        /// <summary>
        /// 生成指定长度的随机ID
        /// </summary>
        /// <param name="noLength">指定序列长度</param>
        /// <returns></returns>
        public static string CreateID(int noLength=15)
        {
			var now = DateTime.Now;

			int totalHours = Convert.ToInt32((now - _markTime).TotalHours);

            string datePrefix = totalHours.ToString("X");

			string timePrefix = now.Minute + now.Millisecond.ToString("X");

            //主键长度-临时中值长度-日期长度
            return string.Format("{0}{1}{2}", datePrefix, timePrefix,
                CreateUniqueString(noLength - (datePrefix+ timePrefix).Length));
        }

        /// <summary>
        /// 生成指定长度的随机ID(不带时间戳前缀)
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string CreateRandomID(int length)
        {
            string result = string.Empty;

            while (result.Length < length)
            {
                result += CreateUniqueNumer(length);
            }

            return result.Length > length ? result.Substring(result.Length - length, length) : result;
        }

        /// <summary>
        /// 创建随机数(不带时间戳纯数字)
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string CreateRandomNumber(int length)
        {
            Random ran = new Random((int) DateTime.Now.Ticks);

            StringBuilder sb = new StringBuilder();

            for (var i = 0; i < length; i++)
            {
                var num=ran.Next(9);

                sb.Append(num.ToString());
            }

            return sb.ToString();

        }
    }
}
